package com.example.smartcity_40.dialog

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ContextMenu
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_40.databinding.DialogAddressBinding
import com.example.smartcity_40.databinding.MItemAddressListBinding
import com.example.smartcity_40.service.takeout.AddressInfoActivity
import com.example.smartcity_40.service.takeout.AddressListBean
import com.example.smartcity_40.tool.GenericAdapter
import com.example.smartcity_40.tool.Tool
import com.example.smartcity_40.tool.Tool.Companion.name
import com.example.smartcity_40.tool.g
import com.example.smartcity_40.tool.tool

class AddressDialog(context: Context,private val listener : OnAddressInfo) : Dialog(context) {
    lateinit var vb : DialogAddressBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        vb = DialogAddressBinding.inflate(layoutInflater)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(vb.root)
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        vb.dialogAddressTb.setOnClickListener {
            dismiss()
        }
        vb.dialogAddressBtn.setOnClickListener {
            context.startActivity(Intent(context,AddressInfoActivity::class.java))
            dismiss()
        }
        loadList()
    }

    private fun loadList() {
        tool.apply {
            send("/prod-api/api/takeout/address/list",Tool.GET,null,true) {
                val data = g.fromJson(it,AddressListBean::class.java).data
                vb.dialogAddressList.adapter = GenericAdapter(data.size,
                        { MItemAddressListBinding.inflate(layoutInflater) }) { binding,position->
                    binding.mItemAddressListName.text = data[position].name
                    binding.mItemAddressListAddressDetail.text = data[position].addressDetail
                    binding.mItemAddressListLabel.text = data[position].label
                    binding.mItemAddressListPhone.text = data[position].phone
                    binding.root.setOnClickListener {
                        listener.onUserInfo(data[position].name,data[position].addressDetail,data[position].label,data[position].phone)
                        dismiss()
                    }
                    binding.mItemAddressListBtn.setOnClickListener {
                        val intent = Intent(context,AddressInfoActivity::class.java)
                        intent.putExtra("name",data[position].name)
                        intent.putExtra("addressDetail",data[position].addressDetail)
                        intent.putExtra("label",data[position].label)
                        intent.putExtra("phone",data[position].phone)
                        intent.putExtra("id",data[position].id)
                        context.startActivity(intent)
                        dismiss()
                    }
                }
                vb.dialogAddressList.layoutManager = LinearLayoutManager(context)
            }
        }
    }
    interface OnAddressInfo {
        fun onUserInfo(name :String,addressDetail : String,label:String,phone:String)
    }
}